import os
import shutil


def rename_files(folder_path, countries, start_index=1):
    # 获取文件夹中所有文件
    all_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if
                 os.path.isfile(os.path.join(folder_path, f))]
    # 按文件创建时间排序
    all_files.sort(key=os.path.getctime)

    current_index = start_index
    group_count = 0
    non_pdf_index = 0
    non_pdf_names = ['A', 'B', 'C']

    for file in all_files:
        file_ext = os.path.splitext(file)[1]
        country = countries[(current_index - 1) % len(countries)]

        if file_ext.lower() == '.pdf':
            new_name = f"{current_index}_{country}_D{file_ext}"
        else:
            new_name = f"{current_index}_{country}_{non_pdf_names[non_pdf_index % 3]}{file_ext}"
            non_pdf_index += 1

        new_path = os.path.join(folder_path, new_name)
        shutil.move(file, new_path)
        print(f"Renamed {file} to {new_path}")

        group_count += 1
        if group_count % 4 == 0:
            current_index += 1
            non_pdf_index = 0

if __name__ == "__main__":
    folder_path = 'F:\\codefordev\\pythondemo\\BitRegister\\image20250325\\unsort'  # 请替换为实际的文件夹路径
    countries = ['Malaysia']
    start_index = 9
    rename_files(folder_path, countries, start_index)